package org.light.oracle.complexverb;

import java.util.ArrayList;
import java.util.List;

import org.light.complexverb.TwoDomainVerb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.exception.ValidateException;
import org.light.oracle.generator.MybatisOracleSqlReflector;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class ListMyActive extends TwoDomainVerb{

	@Override
	public Method generateDaoImplMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
		method.setNoContainer(true);
		List<Writeable> list = new ArrayList<Writeable>();
		if (this.slave instanceof org.light.domain.Enum) {
			list.add(new Statement(100L,1,"<select id=\""+StringUtil.lowerFirst(this.getVerbName())+"\" resultType=\""+this.master.getDomainId().getClassType()+"\"  parameterType=\""+this.master.getDomainId().getClassType()+"\">"));
			list.add(new Statement(200L,2, MybatisOracleSqlReflector.generateSelectActiveIdsUsingMasterIdStatement(master,slave)));
			list.add(new Statement(300L,1,"</select>"));
		} else {
			list.add(new Statement(100L,1,"<select id=\""+StringUtil.lowerFirst(this.getVerbName())+"\" resultMap=\""+this.slave.getLowerFirstDomainName()+"\"  parameterType=\""+this.master.getDomainId().getClassType()+"\">"));
			list.add(new Statement(200L,2, MybatisOracleSqlReflector.generateSelectActiveUsingMasterIdStatement(master,slave)));
			list.add(new Statement(300L,1,"</select>"));
		}
		method.setMethodStatementList(WriteableUtil.merge(list));
		return method;
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
		if (this.slave instanceof org.light.domain.Enum && !"datadummy".equalsIgnoreCase(this.slave.getSchema())) {
			method.setReturnType(new Type("Set<"+this.master.getDomainId().getClassType()+">"));
		} else {
			method.setReturnType(new Type("Set",this.slave, this.slave.getPackageToken()));
		}
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id",this.master.getDomainId().getClassType()));
		method.setThrowException(true);
		return method;
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
		method.setReturnType(new Type("Set",this.slave, this.slave.getPackageToken()));
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id", this.master.getDomainId().getClassType()));
		method.setThrowException(true);
		return method;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));		
		method.setReturnType(new Type("Set",this.slave, this.slave.getPackageToken()));
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id", this.master.getDomainId().getClassType()));
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport("java.util.TreeSet");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.master.getPackageToken()+"."+this.master.getDaoSuffix()+"."+this.master.getStandardName()+"Dao");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getServiceSuffix()+"."+this.slave.getStandardName()+"Service");
		method.setThrowException(true);
		method.addMetaData("Override");
		
		List<Writeable> sList = new ArrayList<Writeable>();
		if (this.slave instanceof org.light.domain.Enum && !"datadummy".equalsIgnoreCase(this.slave.getSchema())) {
			if (this.master instanceof org.light.domain.Enum) {
				sList.add(new Statement(1000L,2,"Set<"+this.master.getDomainId().getClassType()+"> "+this.slave.getLowerFirstDomainName()+"Ids = mtmDao."+StringUtil.lowerFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id);"));
			} else {
				sList.add(new Statement(1000L,2,"Set<"+this.master.getDomainId().getClassType()+"> "+this.slave.getLowerFirstDomainName()+"Ids = dao."+StringUtil.lowerFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id);"));
			}
			sList.add(new Statement(2000L,2,"Set<"+this.slave.getCapFirstDomainNameWithSuffix()+"> _allActiveSet = new TreeSet<>();"));
			sList.add(new Statement(3000L,2,"_allActiveSet.addAll("+this.slave.getLowerFirstDomainName()+"Service.listActive"+this.slave.getCapFirstPlural()+"());"));
			sList.add(new Statement(4000L,2,"Set<"+this.slave.getCapFirstDomainNameWithSuffix()+"> results = new TreeSet<>();"));
			sList.add(new Statement(5000L,2,"for ("+this.slave.getCapFirstDomainNameWithSuffix()+" "+this.slave.getLowerFirstDomainName()+": _allActiveSet){"));
			sList.add(new Statement(6000L,3,"for ("+this.master.getDomainId().getClassType()+" "+this.slave.getLowerFirstDomainName()+"Id : "+this.slave.getLowerFirstDomainName()+"Ids){"));
			sList.add(new Statement(7000L,4,"if ("+this.slave.getLowerFirstDomainName()+".get"+this.slave.getDomainId().getCapFirstFieldName()+"().equals("+this.slave.getLowerFirstDomainName()+"Id)) results.add("+this.slave.getLowerFirstDomainName()+");"));
			sList.add(new Statement(8000L,3,"}"));
			sList.add(new Statement(9000L,2,"}"));
			sList.add(new Statement(10000L,2,"return results;"));
		}else {
			sList.add(new Statement(1000L,2,"Set<"+this.slave.getCapFirstDomainNameWithSuffix()+"> set = new TreeSet<"+this.slave.getCapFirstDomainNameWithSuffix()+">();"));
			if (this.master instanceof org.light.domain.Enum && !"datadummy".equalsIgnoreCase(this.master.getSchema())) {
				sList.add(new Statement(2000L,2,"set.addAll(mtmDao."+StringUtil.lowerFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id"+"));"));	
			} else {
				sList.add(new Statement(2000L,2,"set.addAll(dao."+StringUtil.lowerFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id"+"));"));	
			}
			sList.add(new Statement(3000L,2,"return set;"));
		}
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));		
		method.setReturnType(new Type("Map<String,Object>"));
		method.setThrowException(true);
		method.addAdditionalImport("java.util.List");
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport("java.util.TreeSet");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.master.getPackageToken()+"."+this.master.getServiceSuffix()+"."+this.master.getStandardName()+"Service");
		method.addAdditionalImport(this.master.getPackageToken()+"."+this.master.getServiceimplSuffix()+"."+this.master.getStandardName()+"ServiceImpl");
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id",this.master.getDomainId().getClassType(), this.slave.getPackageToken(),"RequestParam (required = false)"));
		method.addMetaData("RequestMapping(value = \"/"+StringUtil.lowerFirst(method.getStandardName())+"\", method = RequestMethod.POST)");

		List<Writeable> wlist = new ArrayList<Writeable>();
		wlist.add(new Statement(1000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
		wlist.add(new Statement(2000L,2,"Set<"+this.slave.getCapFirstDomainNameWithSuffix()+"> set = new TreeSet<"+this.slave.getCapFirstDomainNameWithSuffix()+">();"));
		wlist.add(new Statement(3000L,2,"if ("+this.master.getLowerFirstDomainName()+"Id"+"!=null) set = service."+StringUtil.lowerFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id"+");"));
		wlist.add(new Statement(4000L,2,"result.put(\"success\",true);"));
		wlist.add(new Statement(5000L,2,"result.put(\"rows\",set);"));
		wlist.add(new Statement(6000L,2,"return result;"));	
		method.setMethodStatementList(WriteableUtil.merge(wlist));
		
		return method;
	}

	public ListMyActive(Domain master,Domain slave) throws ValidateException{
		super();
		this.master = master;
		this.slave = slave;
		this.setVerbName("ListActive"+this.master.getCapFirstDomainName()+StringUtil.capFirst(this.slave.getAliasPlural())+"Using"+this.master.getCapFirstDomainName()+"Id");
		this.setLabel("列出所属");
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		return null;
	}

}